package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R list() {
        // return R.success(new ArrayList<ShoppingCart>());
        //获取用户登录id
        Long useId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper();
        qw.eq(useId != null, ShoppingCart::getUserId, useId);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id,设置用户id
        Long userId = BaseContext.getCurrentId();

        //判断菜品或者套餐是否点过
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(userId != null, ShoppingCart::getUserId, userId);

       /* //说明点的是菜品
        if (shoppingCart.getDishId() != null) {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //如果不是菜品,说明点的是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }*/
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        qw.eq( shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(qw);

        //第一次点的时候是新增,后面再点击的时候是修改
        if (one != null) {
            //更新
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        } else {
            //新增
            shoppingCart.setUserId(userId);
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);

    }

    /**
     * 取消购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        //获取用户登录id
        Long useId = BaseContext.getCurrentId();

        //菜品或者套餐id查询出当前ShoppingCart数据
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(useId != null, ShoppingCart::getUserId, useId);

    /*    //判断是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            //点的是菜品
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //点的是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }*/
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        qw.eq( shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);

        if (number > 0) {
            //
            shoppingCartService.updateById(one);
        } else {

            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R cleanShoppingCart() {
        //获取用户登录id
        Long useId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(useId != null, ShoppingCart::getUserId, useId);
        boolean flag = shoppingCartService.remove(qw);
        return flag ? R.success("清空购物车成功") : R.error("清空购物车失败");
    }


}
